/**
 * 
 */
package info.niwota.js;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

/**
 * @author qiangli
 * 
 */

// "Usage: java -jar yuicompressor-x.y.z.jar [options] [input file]\n\n"
//
// + "Global Options\n"
// + "  -h, --help                Displays this information\n"
// + "  --type <js|css>           Specifies the type of the input file\n"
// + "  --charset <charset>       Read the input file using <charset>\n"
// +
// "  --line-break <column>     Insert a line break after the specified column number\n"
// + "  -v, --verbose             Display informational messages and warnings\n"
// +
// "  -o <file>                 Place the output into <file>. Defaults to stdout.\n\n"
//
// + "JavaScript Options\n"
// + "  --nomunge                 Minify only, do not obfuscate\n"
// + "  --preserve-semi           Preserve all semicolons\n"
// + "  --disable-optimizations   Disable all micro optimizations\n\n"
//
// +
// "If no input file is specified, it defaults to stdin. In this case, the 'type'\n"
// +
// "option is required. Otherwise, the 'type' option is required only if the input\n"
// + "file extension is neither 'js' nor 'css'."

public class YUIJsCompressor extends BaseCompressor implements Compressor {
	public static boolean munge = true;
	public static boolean preserveAllSemiColons = true;
	public static boolean disableOptimizations = true;
	public static boolean verbose = false;
	public static int linebreakpos = -1;

	private static ErrorReporter errorReporter = new ErrorReporter() {
		public void warning(String message, String sourceName, int line,
				String lineSource, int lineOffset) {
			// if (line < 0) {
			// System.err.println("\n[WARNING] " + message);
			// } else {
			// System.err.println("\n[WARNING] " + line + ':' +
			// lineOffset + ':' + message);
			// }
		}

		public void error(String message, String sourceName, int line,
				String lineSource, int lineOffset) {
			// if (line < 0) {
			// System.err.println("\n[ERROR] " + message);
			// } else {
			// System.err.println("\n[ERROR] " + line + ':' +
			// lineOffset + ':' + message);
			// }
		}

		public EvaluatorException runtimeError(String message,
				String sourceName, int line, String lineSource, int lineOffset) {
			// error(message, sourceName, line, lineSource,
			// lineOffset);
			return new EvaluatorException(message);
		}
	};

	@Override
	public void compress(byte[] script, OutputStream os) throws Exception {
		InputStream is = new ByteArrayInputStream(script);
		InputStreamReader in = new InputStreamReader(is);
		try {
			JavaScriptCompressor compressor = new JavaScriptCompressor(in,
					errorReporter);
			OutputStreamWriter out = new OutputStreamWriter(os);
			compressor.compress(out, linebreakpos, munge, verbose,
					preserveAllSemiColons, disableOptimizations);
		} finally {
			in.close();
		}
	}

}
